#!/bin/bash

# Slackware build script for imv

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20210909 bkw:
# - update for v4.3.0.
# - upstream switched to meson, can't build old versions with this script.
# - meson autodetects all the optional libs properly, get rid of the
#   messy shell script code that used to do it.
# - add new optional dep libheif.
# - get rid of squelch_libtiff_warnings.diff (upstream fixed it).
# - rework README.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=imv
VERSION=${VERSION:-4.3.0}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# meson takes an option to say where to install the config file, but
# it doesn't actually change where the binary looks for it.
sed -i -e 's,/etc/imv_config,/etc/imv/imv_config,' \
       -e '/\/usr\/local\/etc/d' \
    src/imv.c doc/imv.1.txt

mkdir build
cd build
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \
  meson .. \
    --buildtype=release \
    --infodir=/usr/info \
    --libdir=/usr/lib${LIBDIRSUFFIX} \
    --localstatedir=/var \
    --mandir=/usr/man \
    --prefix=/usr \
    --sysconfdir=/etc/$PRGNAM \
    -Dstrip=true
  "${NINJA:=ninja}"
  DESTDIR=$PKG $NINJA install
cd ..

gzip -9 $PKG/usr/man/man?/*.?
mv $PKG/etc/$PRGNAM/${PRGNAM}_config $PKG/etc/$PRGNAM/${PRGNAM}_config.new

# Slackware doesn't have an icon called multimedia-photo-viewer...
mkdir -p $PKG/usr/share/applications
sed -i "s,multimedia-photo-viewer,$PRGNAM," \
    $PKG/usr/share/applications/*.desktop

# Icon came from Debian moka-icon-theme package. I have no idea
# what the default icon theme is in Debian (equivalent of hicolor in
# Slackware). Don't care, either. So I picked one at random.
for i in 16 22 24 48 64 96; do
  mkdir -p $PKG/usr/share/icons/hicolor/${i}x${i}/apps
  cat $CWD/icons/$i.png > $PKG/usr/share/icons/hicolor/${i}x${i}/apps/$PRGNAM.png
done
mkdir -p $PKG/usr/share/pixmaps
ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS* CHANGE* CONTRIB* LICENSE* README* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install

# update slack-desc to say which optional deps are included
F=no; G=no; H=no
ldd $PKG/usr/bin/$PRGNAM-x11 > lddtmp
grep -q 'libfreeimage' lddtmp && F=yes
grep -q 'libnsgif' lddtmp && G=yes
grep -q 'libheif' lddtmp && H=yes

sed -e "s,@F@,$F," \
    -e "s,@G@,$G," \
    -e "s,@H@,$H," \
    $CWD/slack-desc > $PKG/install/slack-desc

cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
